﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GalaSoft.MvvmLight;
using SWCInvoice.Model;
using System.Collections.ObjectModel;
using System.Windows;
using Microsoft.Windows.Controls;
//using System.Windows.Controls;

namespace SWCInvoice.ViewModel
{
    public class MessageViewModel : ViewModelBase
    {
        #region Fields

        /// <summary>
        /// The list of messages
        /// </summary>
        ObservableCollection<Message> _messages;

        /// <summary>
        /// Collapse/Expand the details
        /// </summary>
        private RelayCommand _DetailRowVisibilityCommand;

        #endregion

        #region Properties

        /// <summary>
        /// The list of the messages
        /// </summary>
        public ObservableCollection<Message> Messages {
            get {
                return _messages == null ? _messages = new ObservableCollection<Message>() : _messages;
            }
            set {
                if (_messages == value)
                    return;
                _messages = value;
                RaisePropertyChanged("Messages");
            }
        }

        #endregion

        #region Commands

        public RelayCommand DetailRowVisibilityCommand {
            get {
                if (_DetailRowVisibilityCommand == null)
                    _DetailRowVisibilityCommand = new RelayCommand(param => DetailRowVisibility_Click(param));
                return _DetailRowVisibilityCommand;
            }
        }

        #endregion

        #region Command Methods

        /// <summary>
        /// Collapse/Expand the details
        /// </summary>
        /// <param name="sender">The Datagrid</param>
        private void DetailRowVisibility_Click(object sender) {
            DataGrid grid = ((DataGrid)sender);
            DataGridRow row = (DataGridRow)grid.ItemContainerGenerator.ContainerFromIndex(grid.SelectedIndex);                               

            row.DetailsVisibility = row.DetailsVisibility == Visibility.Collapsed ?
                Visibility.Visible : Visibility.Collapsed;

            grid.SelectedIndex = -1;
        }

        #endregion

    }
}
